﻿using MainView.Model;
using MainView.Model.sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainView.MyControl
{
    public partial class DevCommon : UserControl
    {
        public RoomState Rs { get; set; }

        string[] state = {"关闭","开启"};
        Color safe = Color.FromArgb(0, 192, 0);
        Color danger = Color.Crimson;
        private bool fanSta = false;
        private bool flueSta = false;
        public bool isOnline = false;

        public DevCommon(RoomState roomState)
        {
            InitializeComponent();
            Rs = roomState;
            UpShowFirst();
        }

        public void UpShowFirst()
        {
            Action DoAction = delegate ()
            {
                RoomNamelbl.Text = Rs.Name;
                UpShowOnline(false);
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpChangePic(int id)
        {
            Action DoAction = delegate ()
            {
                if (id == 0) pictureBox1.Image = global::MainView.Properties.Resources.新房间剖视有背景;
                if (id == 2) pictureBox1.Image = global::MainView.Properties.Resources.瞻仰厅;
                if (id == 4) pictureBox1.Image = global::MainView.Properties.Resources.前厅;
                if (id == 5) pictureBox1.Image = global::MainView.Properties.Resources.后厅;

            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowSpeed(double speed)
        {
            Action DoAction = delegate ()
            {
                Speedlbl.Text = speed.ToString()+" m/s";
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowOnline(bool isonline)
        {
            Action DoAction = delegate ()
            {
                if (isonline == true)//在线
                {
                    label2.Visible = false;
                    RealStalbl.Text = "在线";
                    RealStalbl.ForeColor = safe;
                    isOnline = true;
                    outlinePic.Visible = false;
                }
                else
                {
                    label2.Visible = true;
                    RealStalbl.Text = "离线";
                    RealStalbl.ForeColor = danger;
                    isOnline = false;
                    outlinePic.Visible = true;
                    UpShowAlarm(false);
                    UpShowFan(false);
                    UpShowFlue(false);
                    UpShowLight(false);
                    UpShowUV(false);
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFan(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                if (isOpen == true)//正常
                {
                    fanSta = true;
                    ShowAirflowPic(fanSta, flueSta);
                    FanStalbl.Text = state[1];
                    FanStalbl.ForeColor = safe;
                    ucFanBlower1.TurnAround = HZH_Controls.Controls.TurnAround.Clockwise;
                    ucFanConduit1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.Backward;
                    ucFanConduit2.LiquidDirection = HZH_Controls.Controls.LiquidDirection.Forward;
                    ucFanConduit3.LiquidDirection = HZH_Controls.Controls.LiquidDirection.Forward;
                }
                else
                {
                    fanSta = false;
                    ShowAirflowPic(fanSta, flueSta);
                    FanStalbl.Text = state[0];
                    FanStalbl.ForeColor = danger;
                    ucFanBlower1.TurnAround = HZH_Controls.Controls.TurnAround.None;
                    ucFanConduit1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.None;
                    ucFanConduit2.LiquidDirection = HZH_Controls.Controls.LiquidDirection.None;
                    ucFanConduit3.LiquidDirection = HZH_Controls.Controls.LiquidDirection.None;

                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFlue(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                if (isOpen == true)//正常
                {
                    flueSta = true;
                    ShowAirflowPic(fanSta, flueSta);
                    FluePic.Image= global::MainView.Properties.Resources.烟囱;
                    ValveStalbl.Text= state[1];
                    ValveStalbl.ForeColor = safe;
                    FlueStalbl.Text = state[1];
                    FlueStalbl.ForeColor = safe;
                    ucFlueBlower1.TurnAround = HZH_Controls.Controls.TurnAround.Clockwise;
                    ucFlueValve1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.Forward;
                    ucFlueConduit1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.Backward;
                    

                }
                else
                {                   
                    flueSta = false;
                    ShowAirflowPic(fanSta,flueSta);
                    FluePic.Image = global::MainView.Properties.Resources.无烟烟囱;
                    ValveStalbl.Text = state[0];
                    ValveStalbl.ForeColor = danger;
                    FlueStalbl.Text = state[0];
                    FlueStalbl.ForeColor = danger;
                    ucFlueBlower1.TurnAround = HZH_Controls.Controls.TurnAround.None;
                    ucFlueValve1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.None;
                    ucFlueConduit1.LiquidDirection = HZH_Controls.Controls.LiquidDirection.None;
                    

                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowLight(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                if (isOpen == true)//正常
                {
                    LightStalbl.Text = state[1];
                    LightStalbl.ForeColor = safe;
                    LightStaPic.Image= global::MainView.Properties.Resources.灯_照明;
                }
                else
                {
                    LightStalbl.Text = state[0];
                    LightStalbl.ForeColor = danger;
                    LightStaPic.Image = global::MainView.Properties.Resources.灯_关;
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowUV(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                if (isOpen == true)//正常
                {
                    UVStalbl.Text = state[1];
                    UVStalbl.ForeColor = safe;
                    UVStaPic.Image = global::MainView.Properties.Resources.灯_紫改;
                }
                else
                {
                    UVStalbl.Text = state[0];
                    UVStalbl.ForeColor = danger;
                    UVStaPic.Image = global::MainView.Properties.Resources.灯_关;
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowAlarm(bool isOk)
        {
            Action DoAction = delegate ()
            {
                if (isOk == true)//报警
                {
                    Alarmlbl.Visible = true;
                    ucAlarmLamp1.Visible = true;
                }
                else
                {
                    Alarmlbl.Visible = false;
                    ucAlarmLamp1.Visible = false;                   
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {           
            pictureBox2.Visible = !pictureBox2.Visible;
        }

        private void ShowAirflowPic(bool fan, bool flue)
        {
            if (fan == true && flue == true)
            {                
                pictureBox2.Image= global::MainView.Properties.Resources.循环出气无背景;
                timer1.Enabled = true;
            }
            else if (fan == true && flue == false)
            {
                timer1.Enabled = true;
                pictureBox2.Image = global::MainView.Properties.Resources.循环气流无背景;
            }
            else if (fan == false && flue == true)
            {
                timer1.Enabled = true;
                pictureBox2.Image = global::MainView.Properties.Resources.出气无背景;
            }
            else
            {
                timer1.Enabled = false;
                pictureBox2.Visible = false;
            }

        }
    }
}
